home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / aijournl / 1987_02 / lispvc.feb < prev   
Text File  |  1987-01-29  |  3KB  |  87 lines

  1.  
  2.  
  3.                          LISP vs. C
  4.                For Implementing Expert Systems
  5.                         February 1987
  6.                        Listings 1 & 2
  7.  
  8. Listing 1.
  9.  
  10. This example illustrates several aspects of LISP that
  11. are useful in building expert systems.  Symbols are to represent
  12. information such as room types and as variables to hold specific
  13. rooms and doors.  Lists aggregate related information, doors with
  14. their corresponding rooms.  The MAKE-... functions automatically
  15. allocate the necessary memory to hold the structures.  When a
  16. structure becomes unaccessible, it is reclaimed by LISP's garbage
  17. collector. 
  18.  
  19.  
  20. ;;; We will now create a collections of structures that represent
  21. ;;; 2 rooms a hall and a bathroom.  All halls will open onto the
  22. ;;; hall.
  23.  
  24. ;;; The structure for information about a room.
  25. (defstruct room
  26.   type            ;the kind of room, eg, BEDROOM
  27.   doors-to-rooms  ;doors and the rooms they lead to
  28.   )
  29.   
  30. ;;; The structure for information about a door.
  31. (defstruct door
  32.   type            ;the type of door
  33.   height          ;in inches
  34.   width           :in inches
  35.   )
  36.  
  37. ;;; Make the hall.
  38. (setf hall (make-room :type 'hall))    ;no connecting rooms yet
  39.  
  40. ;;; The door from bedroom1 to the hall.
  41. (setf bedroom1-door (make-door :type 'oak :height 72 :width 40))
  42.  
  43. ;;; The 1st bedroom with its door to the hall
  44. (setf bedroom1 (make-room :type 'bedroom
  45.                            :doors-to-rooms (list bedroom1-door
  46.                                                  hall)))
  47.  
  48. ;;; The door from bedroom2 to the hall.
  49. (setf bedroom2-door (make-door :type 'maple :height 72 :width 40))
  50.  
  51. ;;; The 2nd bedroom with its door to the hall
  52. (setf bedroom2 (make-room :type 'master-bedroom
  53.                           :doors-to-rooms (list bedroom2-door
  54.                                                 hall)))
  55.  
  56. ;;; The Bathroom door
  57. (setf bathroom-door (make-door :type 'pine :height 72 :width 40))
  58.  
  59. ;;; The bathroom with its door to the hall
  60. (setf bathroom (make-room :type 'bathroom
  61.                           :doors-to-rooms (list bathroom-door
  62.                                                 hall)))
  63.  
  64. ;;; Now we connect the hall to its corrensponding rooms
  65. (setf (room-doors-to-rooms hall)
  66.       (list bathroom-door bathroom
  67.             bedroom1-door bedroom1
  68.             bedroom2-door bedroom2))
  69.  
  70.  
  71.  
  72. Listing 2.
  73. ;;; An example of the use of a macro.  This is from an assembler,
  74. ;;; it is used to build the necessary databases to assemble the
  75. ;;; JLE and JNG instructions during PASS2 of the assembler.
  76. (PASS2 (JLE JNG) #B01111110 CONDITIONAL-BRANCH)
  77.  
  78. ;;; This is what the above macro translates into to.  As you can
  79. ;;; see, the above form is more clear to the reader as well as
  80. ;;; easier for the programmer to type.
  81. (PROGN 
  82.   (SETF (GET 'JNG 'BIT-CODE) '126)
  83.   (SETF (GET 'JNG 'PASS2) 'CONDITIONAL-BRANCH)
  84.   (SETF (GET 'JLE 'BIT-CODE) '126)
  85.   (SETF (GET 'JLE 'PASS2) 'CONDITIONAL-BRANCH))
  86. F (GET 'JNG 'PASS2) 'CONDITIONAL-BRANCH)
  87.   (SETF (GET 'JLE 'BIT-CODE) '126)